package com.yunti.business.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunti.business.mapper.BaseSchoolMapper;
import com.yunti.business.domain.BaseSchool;
import com.yunti.business.service.IBaseSchoolService;

/**
 * 学校Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class BaseSchoolServiceImpl implements IBaseSchoolService
{

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private BaseSchoolMapper baseSchoolMapper;

    /**
     * 查询学校
     *
     * @param id 学校主键
     * @return 学校
     */
    @Override
    public BaseSchool selectBaseSchoolById(Long id)
    {
        BaseSchool baseSchool = baseSchoolMapper.selectBaseSchoolById(id);
        Long num =  baseSchoolMapper.selectBaseSchoolStuNum(id);
        baseSchool.setStuNum(num);
        return baseSchool;
    }

    /**
     * 查询学校列表
     *
     * @param baseSchool 学校
     * @return 学校
     */
    @Override
    public List<BaseSchool> selectBaseSchoolList(BaseSchool baseSchool)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long raceId = loginUser.getSelectRaceId();
        baseSchool.setRaceId(raceId);
        return baseSchoolMapper.selectBaseSchoolList(baseSchool);
    }

    /**
     * 新增学校
     *
     * @param baseSchool 学校
     * @return 结果
     */
    @Override
    public int insertBaseSchool(BaseSchool baseSchool)
    {
        baseSchool.setCreateTime(LocalDateTime.now());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long raceId = loginUser.getSelectRaceId();
        baseSchool.setRaceId(raceId);
        return baseSchoolMapper.insertBaseSchool(baseSchool);
    }

    /**
     * 修改学校
     *
     * @param baseSchool 学校
     * @param operName
     * @return 结果
     */
    @Override
    public int updateBaseSchool(BaseSchool baseSchool, String operName)
    {
        baseSchool.setUpdateBy(operName);
        baseSchool.setUpdateTime(LocalDateTime.now());
        return baseSchoolMapper.updateBaseSchool(baseSchool);
    }

    /**
     * 批量删除学校
     *
     * @param ids 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deleteBaseSchoolByIds(Long[] ids)
    {
        return baseSchoolMapper.deleteBaseSchoolByIds(ids);
    }

    /**
     * 删除学校信息
     *
     * @param id 学校主键
     * @return 结果
     */
    @Override
    public int deleteBaseSchoolById(Long id)
    {
        return baseSchoolMapper.deleteBaseSchoolById(id);
    }


    /**
     * 导入学校数据
     *
     * @param baseSchoolList 学校数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importSchool(List<BaseSchool> baseSchoolList, boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(baseSchoolList) || baseSchoolList.size() == 0)
        {
            throw new ServiceException("导入学校数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long raceId = loginUser.getSelectRaceId();
        for (BaseSchool baseSchool : baseSchoolList)
        {
            try
            {
                baseSchool.setRaceId(raceId);
                // 验证是否存在这个用户
                BaseSchool oldBaseSchool = baseSchoolMapper.selectBaseSchoolByName(baseSchool);
                if (StringUtils.isNull(oldBaseSchool))
                {
                    baseSchool.setCreateBy(operName);
                    baseSchool.setCreateTime(LocalDateTime.now());
                    baseSchoolMapper.insertBaseSchool(baseSchool);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + baseSchool.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    oldBaseSchool.setUpdateBy(operName);
                    oldBaseSchool.setUpdateTime(LocalDateTime.now());
                    baseSchoolMapper.updateBaseSchool(oldBaseSchool);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + oldBaseSchool.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + baseSchool.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + baseSchool.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
